package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SaveTR
 */
//@WebServlet(description = "儲存輸入的測試結果", urlPatterns = { "/SaveTR" })
public class SaveTR extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveTR() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");		
		Widgets wid = new Widgets();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String strSQL = "INSERT INTO tms_mrtr (tpid,mrid,tcuid,ver,trSN,edition,retenum,succnum,failnum,cantnum,waitnum,creator,creatorGroup,createdTime) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
				
		String returnResult = "OK";
		
		String edition = request.getParameter("edition");
		int retestNum = Integer.parseInt(request.getParameter("tr_retest"));
		int succNum = Integer.parseInt(request.getParameter("tr_Succ"));
		int failNum = Integer.parseInt(request.getParameter("tr_Fail"));
		int cantNum = Integer.parseInt(request.getParameter("tr_Cant"));
		int trSN  = Integer.parseInt(request.getParameter("trSN"));
		int ver = Integer.parseInt(request.getParameter("ver"));
		String mrid = request.getParameter("mrid");
		int tpid = Integer.parseInt(request.getParameter("tpid"));
		String creator = request.getParameter("loginUser");
		String creatorGroup = request.getParameter("submittedBy");
		int tcuid = Integer.parseInt(request.getParameter("tcuid"));
		int numKeyPoint = Integer.parseInt(request.getParameter("numKeyPoint"));
		int waitNum = numKeyPoint-(succNum+failNum+cantNum);
		
		try {
			conn = wid.getConn(true);
			ps = conn.prepareStatement(strSQL);
			ps.setInt(1, tpid);
			ps.setString(2, mrid);
			ps.setInt(3, tcuid);
			ps.setInt(4, ver);
			ps.setInt(5, trSN);
			ps.setString(6, edition);
			ps.setInt(7, retestNum);
			ps.setInt(8, succNum);
			ps.setInt(9, failNum);
			ps.setInt(10,cantNum);
			ps.setInt(11,waitNum );
			ps.setString(12, creator);
			ps.setString(13, creatorGroup);
			ps.setTimestamp(14, wid.currentTimeStamp());
			ps.execute();
			
			wid.checkTRsubmittAble(tpid, mrid, ver, conn, ps, rs);
			
		} catch (SQLException e) {
			returnResult = wid.keepErrorMes(conn, ps, e, "", true);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				returnResult = e.toString();
			}
		}
		
		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(returnResult);
	}
}
